home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / CopyMask / CopyMask.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.0 KB  |  129 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        CopyMask.c
  3.  
  4.     Contains:    Shows how CopyMask can used to fade a screen to a lighter color.    
  5.  
  6.     Written by: JW
  7.  
  8.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  20.                                             for demonstration purposes.
  21.                 7/9/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #include    "CarbonPrefix.h"
  26. #include     <Dialogs.h>
  27. #include    <Fonts.h>
  28. #include     <Processes.h>
  29. #include    <QDOffscreen.h>
  30. #include    <Sound.h>
  31. #include    <Gestalt.h>
  32.  
  33. void main(void)
  34. {
  35.     WindowPtr        mainWinPtr;
  36.     OSErr            error;
  37.     //SysEnvRec        theWorld;    //not available in carbon
  38.     long            result;        //used in carbonization
  39.     GWorldPtr        myOffscreen1, myOffscreen2;
  40.     GDHandle        oldDevice;
  41.     CGrafPtr        oldPort;
  42.     Rect            windRect, offRect, myRect;
  43.     RGBColor        theColor;
  44.     int                i;
  45.     RgnHandle        rgnHandle = NewRgn();
  46.     
  47.     /* Make sure ColorQD exists. */
  48.     /*error = SysEnvirons(1, &theWorld);
  49.     if (theWorld.hasColorQD == false) {
  50.         SysBeep(50);
  51.         ExitToShell();
  52.     }*/
  53.     
  54.     error = Gestalt(gestaltQuickdrawVersion, &result);
  55.     if (error != noErr || result < gestalt8BitQD) {
  56.         SysBeep(50);
  57.         ExitToShell();
  58.     }
  59.     
  60.     /* Initialize all the needed managers. */
  61.     /*InitGraf(&qd.thePort);
  62.     InitFonts();
  63.     InitWindows();
  64.     InitMenus();
  65.     TEInit();
  66.     InitDialogs(nil);*/
  67.     InitCursor();
  68.  
  69.     /* Define output window. */
  70.     SetRect(&windRect, 100, 100, 400, 400);
  71.     SetRect(&offRect, 0, 0, 300, 300);
  72.     mainWinPtr = NewCWindow(nil, &windRect, "\pJohn", true, documentProc, (WindowPtr) -1, false, 0);
  73.     //SetPort(mainWinPtr);
  74.     SetPortWindowPort(mainWinPtr);
  75.     GetGWorld(&oldPort, &oldDevice);
  76.  
  77.     /* Create two offscreen GWorlds. */
  78.     if (NewGWorld(&myOffscreen1, 0, &offRect, nil, nil, 0))
  79.         Debugger();
  80.     if (NewGWorld(&myOffscreen2, 0, &offRect, nil, nil, 0))
  81.         Debugger();
  82.     
  83.     /* Now, draw ramp to first offscreen pixmap. */
  84.     SetGWorld(myOffscreen1, nil);
  85.     for (i=0; i<10; i++) {
  86.         theColor.red = theColor.green = theColor.blue = (i*28) << 8;
  87.         RGBForeColor(&theColor);
  88.         SetRect(&myRect, 0, i*30, 300, i*30+30);
  89.         PaintRect(&myRect);
  90.     }
  91.  
  92.     /* Now, draw mask to second offscreen pixmap. */
  93.     SetGWorld(myOffscreen2, nil);
  94.     SetRect(&myRect, 0, 0, 300, 300);
  95.     theColor.red = theColor.green = theColor.blue = (128) << 8;
  96.     RGBForeColor(&theColor);
  97.     PaintRect(&myRect);
  98.  
  99.     /*    Draw to the ramp to the window.    */
  100.     SetGWorld(oldPort, oldDevice);
  101.     /*CopyBits((BitMapPtr) *myOffscreen1->portPixMap, &(*mainWinPtr).portBits, &offRect,
  102.                 &offRect, 0, nil);*/
  103.     CopyBits((BitMapPtr) *(GetPortPixMap(myOffscreen1)), GetPortBitMapForCopyBits(GetWindowPort(mainWinPtr)), &offRect,
  104.                 &offRect, 0, nil);
  105.     QDFlushPortBuffer(GetWindowPort(mainWinPtr), GetPortVisibleRegion(GetWindowPort(mainWinPtr), rgnHandle));
  106.  
  107.     /* Wait until user clicks button. */
  108.     do {
  109.     } while (!Button());
  110.     
  111.     /*    Now fade the window to white using CopyMask.  The destination must be erased
  112.         for CopyMask to work.    */
  113.     SetGWorld(oldPort, oldDevice);
  114.     EraseRect(&offRect);
  115.     /*CopyMask((BitMapPtr) *myOffscreen1->portPixMap, (BitMapPtr) *myOffscreen2->portPixMap,
  116.                 &(*mainWinPtr).portBits, &offRect, &offRect, &offRect);*/
  117.     CopyMask((BitMapPtr) *(GetPortPixMap(myOffscreen1)), (BitMapPtr) *(GetPortPixMap(myOffscreen2)),
  118.                 GetPortBitMapForCopyBits(GetWindowPort(mainWinPtr)), &offRect, &offRect, &offRect);
  119.     QDFlushPortBuffer(GetWindowPort(mainWinPtr), GetPortVisibleRegion(GetWindowPort(mainWinPtr), rgnHandle));
  120.  
  121.     /* Wait until user clicks button. */
  122.     do {
  123.     } while (Button());
  124.     do {
  125.     } while (!Button());
  126.     
  127.     DisposeRgn(rgnHandle);
  128. }
  129.